/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListFactorsPresenter.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.EditFactorEvent;
import com.ingenius.sau.client.event.ListFeaturesEvent;
import com.ingenius.sau.client.service.GuidelinesServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.guidelines.Factor;
import com.ingenius.sau.shared.model.guidelines.Guideline;
import com.ingenius.sau.shared.util.filter.BeanFilter;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>ListFactorsPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListFactorsPresenter implements ListPresenter<Factor> {

	public interface ListFactorsDisplay extends Display {
		void reload();
	}

	private final HandlerManager eventBus;
	private final GuidelinesServiceAsync rpcService;
	private final ListFactorsDisplay display;
	private String selectedFactorId;

	/**
	 * 
	 * @param eventBus
	 * @param rpcService
	 * @param display
	 */
	public ListFactorsPresenter(HandlerManager eventBus,
			GuidelinesServiceAsync rpcService, ListFactorsDisplay display) {

		this.eventBus = eventBus;
		this.rpcService = rpcService;
		this.display = display;

		display.setPresenter(this);
		selectedFactorId = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doAdd()
	 */
	@Override
	public void doAdd() {
		fireEvent(new EditFactorEvent(null));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doEdit()
	 */
	@Override
	public void doEdit() {
		fireEvent(new EditFactorEvent(selectedFactorId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doDelete()
	 */
	@Override
	public void doDelete() {
		// TODO i18n
		final String failedMessage = "Failed to delete factor";
		final String successMessage = "Factor deleted sucessfully";

		AppController.setStatusMessage("Deleting...");
		rpcService.deleteFactor(selectedFactorId, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_DELETE
						+ Factor.class.getName(), caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result.booleanValue() == true) {
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
				display.reload();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#goInto()
	 */
	@Override
	public void goInto() {
		AppController.setAttribute(Constants.CURRENT_FACTOR_ID,
				selectedFactorId);
		fireEvent(new ListFeaturesEvent());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.ListPresenter#fireEvent()
	 */
	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#setSelectedItemId
	 * (java.lang.String)
	 */
	@Override
	public void setSelectedItemId(String id) {
		this.selectedFactorId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#loadData(java.util
	 * .List, java.lang.Object, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void loadData(List<Filter> filters, Object loadConfig,
			AsyncCallback<PagingLoadResult<Factor>> callback) {

		String currentGuidelineId = (String) AppController
				.getAttribute(Constants.CURRENT_GUIDELINE_ID);
		if (currentGuidelineId != null) {
			filters = (filters == null) ? filters = new ArrayList<Filter>()
					: filters;
			filters.add(new BeanFilter(Comparison.EQUALS, "guideline",
					new Guideline(currentGuidelineId)));
		}
		rpcService.findFactors(filters, (BasePagingLoadConfig) loadConfig,
				callback);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.Presenter#go(com.google.gwt.user
	 * .client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public String getModuleName() {
		return Constants.TOKEN_GUIDELINES;
	}

}
